package com.example.ws.handler.chess;

import io.javalin.apibuilder.ApiBuilder;
import io.javalin.apibuilder.EndpointGroup;
import io.javalin.http.Context;

import java.util.Objects;

public class ChessHandler implements EndpointGroup {

    private static BoxChess boxChess = new BoxChess(15, 15);

    @Override
    public void addEndpoints() {
        ApiBuilder.get("", ChessHandler::chessDown);
        ApiBuilder.get("", ChessHandler::chessStart);
    }

    public static void chessDown(Context ctx) {
        int x = Integer.parseInt(Objects.requireNonNull(ctx.queryParam("x")));
        int y = Integer.parseInt(Objects.requireNonNull(ctx.queryParam("y")));
        int player = Integer.parseInt(Objects.requireNonNull(ctx.queryParam("player")));
        int result = boxChess.down(player, new Pos(x, y));
        ctx.result("player[" + result + "] win!!");
    }

    public static void chessStart(Context ctx) {
        boxChess = new BoxChess(15, 15);
        ctx.result("OK");
    }
}
